/**
 * Project Name:MVC
 * File Name:LoginWithJSONController.java
 * Package Name:cn.com.tupperware.controller
 * Date:Nov 9, 20164:37:51 PM
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 *
*/

package cn.com.tupperware.controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import cn.com.tupperware.dao.AccountMapper;
import cn.com.tupperware.entity.Account;
import cn.com.tupperware.entity.AccountExample;
import cn.com.tupperware.entity.AccountExample.Criteria;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * ClassName:LoginWithJSONController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: Nov 9, 2016 4:37:51 PM <br/>
 * 
 * @author upw023
 * @version
 * @since JDK 1.6
 * @see
 */
@Controller

@SessionAttributes("currUser")
public class LoginWithJSONController {
	@Autowired
	private AccountMapper accountMapper;

	@RequestMapping(value = { "/login.do" }, method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String loginWithJSON(@ModelAttribute Login login, ModelMap model) throws IOException {
		// System.out.println(login.getPassword());
		AccountExample ex = new AccountExample();
		Criteria criteria = ex.createCriteria();
		Account account = null;
		criteria.andUsernameEqualTo(login.getUserName());
		criteria.andPasswordEqualTo(login.getPassword());

		List<Account> ac = accountMapper.selectByExample(ex);
		Iterator<Account> iter = ac.iterator();
		JSONArray array = new JSONArray();
		JSONObject json = new JSONObject();
		if (iter.hasNext()) {
			json.put("islogin", true);

			account = (Account) iter.next();
			
			model.addAttribute("currUser", account);
			System.out.println(account.toString());
			// model.addAttribute("currUser", login.getUserName());
		} else {
			json.put("islogin", false);
		}
		array.add(json);
		// model.addAttribute("currUser", json);
		// System.out.println(array.toString());
		return array.toString();
	}

	@RequestMapping(value = { "/home.do" }, method = RequestMethod.GET, produces = "text/json;charset=UTF-8")

	public ModelAndView home(HttpSession session) throws Exception {
		ModelAndView mav = new ModelAndView("home");
		Account account = (Account) session.getAttribute("currUser");
		System.out.println(account.toString());
		return mav;

	}

	@RequestMapping(value = { "/logout.do" }, method = RequestMethod.GET)
	public String logout(HttpServletResponse response, ModelMap model, HttpSession session) {
		session.removeAttribute("currUser");

		model.remove("currUser");
		model.clear();
		session.invalidate();
		return "../../index";
	}
}
